﻿using PersonBlog.Model;
using PersonBlog.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PersonBlog.Models;
using System.Data;
using PersonBlog.Common;
using PWDManage.Model.QueryParam;

namespace PersonBlog.Controllers
{
    public class PwdManageController : Controller
    {
        PwdBLL pwdBll = new PwdBLL();
        
        public ActionResult Index()
        {
            QueryParamBase query = new QueryParamBase();
            query.PageCount = 1;
            query.PageSize = 8;
            int TotalCount = 0;
            DataSet ds = pwdBll.GetListByPage(query,out TotalCount);
            if (ds.Tables.Count<=0)
            {
                return View();
            }
            List<PwdModel> pwdList = DataCache.DataSetToEntityList<PwdModel>(ds, 0).ToList();
            ViewBag.PwdList = pwdList;
            ViewBag.TotalCount = TotalCount;
            return View(query);
        }
        [HttpPost]
        public ActionResult Index(QueryParamBase query)
        {
            int TotalCount = 0;
            DataSet ds = pwdBll.GetListByPage(query, out TotalCount);
            if (ds.Tables.Count <= 0)
            {
                return View();
            }
            List<PwdModel> pwdList = DataCache.DataSetToEntityList<PwdModel>(ds, 0).ToList();
            ViewBag.PwdList = pwdList;
            ViewBag.TotalCount = TotalCount;
            return View(query);
        }

        [HttpGet]
        public ActionResult Create(string Id)
        {
            PwdModel pm = new PwdModel();
            if (!string.IsNullOrWhiteSpace(Id))
            {
                pm = pwdBll.GetModel(Convert.ToInt32(Id));
            }
            return View(pm);
        }

        [HttpPost]
        public ActionResult Create(PwdModel pm)
        {
            pm.CreateTime = DateTime.Now;
            if (pm.ID > 0)
            {
                if (pwdBll.Update(pm))
                {
                    return RedirectToAction("Index", "PwdManage");
                }
                else
                {
                    Result r = new Result();
                    r.isSuccess = false;
                    r.Message = "保存失败！";
                    ViewBag.Result = r;
                    return View();
                }
            }
            else
            {
                if (pwdBll.Add(pm) > 0)
                {
                    return RedirectToAction("Index", "PwdManage");
                }
                else
                {
                    Result r = new Result();
                    r.isSuccess = false;
                    r.Message = "保存失败！";
                    ViewBag.Result = r;
                    return View();
                }
            }
           
            
        }

        [HttpGet]
        public ActionResult Delete(string ids)
        {
            if (string.IsNullOrWhiteSpace(ids))
            {
                return RedirectToAction("Index");
            }
            string idList = ids.TrimEnd(',');
            if (pwdBll.DeleteList(idList))
            {
                return RedirectToAction("Index");
            }
            else
            {
                Result r = new Result();
                r.isSuccess = false;
                r.Message = "删除失败！";
                ViewBag.Result = r;
                return RedirectToAction("Index");
            }
        }

        public ActionResult test()
        {
            return View();
        }
    }
}